package org.testis.utils;

import javax.jms.JMSException;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 *
 * @author nurhat
 */
public class AtofActiveMqConnectionFactory extends ActiveMQConnectionFactory {

    private static final long serialVersionUID = 198211198211198211L;
    private static AtofActiveMqConnectionFactory m_connection = null;

    private AtofActiveMqConnectionFactory() {
        String activemqUrl = "failover:(tcp://" + UserSettingsManager.getInstance().getActiveMqUrl() + ":61616?wireFormat.maxInactivityDuration=-1&keepAlive=true)?backup=true&maxReconnectDelay=100";
        setBrokerURL(activemqUrl);
        setUseCompression(true);
        setCloseTimeout(DEFAULT_PRODUCER_WINDOW_SIZE);
        System.out.println("Current Broker Url:" + activemqUrl + " is using compression:" + isUseCompression());
    }

    public static AtofActiveMqConnectionFactory createInstance() {
        if (m_connection == null) {
            m_connection = new AtofActiveMqConnectionFactory();
        }
        return m_connection;
    }

    public static boolean checkActiveMqConnection(String activemqUrl) {

        String url = activemqUrl;
        ActiveMQConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory(url);

        try {
            javax.jms.Connection con =
                    connectionFactory.createConnection();

            con.start();
            con.stop();
            System.out.println("Connection established to activemq "
                    + activemqUrl + "...");
            return true;
        } catch (JMSException ex) {
            System.out.println("Activemq Connection couldn't be established to "
                    + url);
        }
        return false;
    }
}
